package com.dd.keel.core.common.utils;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Iterator;

public class CollectionUtil {
	
	public static Properties transferKeyToUppercase(Properties attributes)
	{
		if(attributes==null) return null;
		Properties newAttributes = new Properties();
		Iterator keyIter = attributes.keySet().iterator();
		while(keyIter.hasNext())
		{
			String key = (String)keyIter.next();
			String value = attributes.getProperty(key);
			key = key.toUpperCase();
			if(value!=null) newAttributes.put(key, value);
		}
		return newAttributes;
	}
	
	public static String getValueByUppercaseKey(Properties attributes,String oldkey)
	{
		if(oldkey==null || attributes==null || attributes.isEmpty()) return null;
		oldkey = oldkey.toUpperCase();
		return attributes.getProperty(oldkey);
	}
	
	public static Properties getPropertiesWithPrefix(Properties props,String namePrefix)
	{
		Properties result = new Properties();
		if(props==null || props.isEmpty())
		{
			return result;
		}
		Enumeration keyEnum = props.keys();
		while(keyEnum.hasMoreElements())
		{
			String key = (String)keyEnum.nextElement();
			if(key.startsWith(namePrefix))
			{
				String value = props.getProperty(key);
				value = EtcConfig.filter(value);
				result.put(key, value);
			}
		}
		return result;
	}
	
	public static Properties transformProperties(Properties props)
	{
		Properties result = new Properties();
		if(props==null || props.isEmpty())
		{
			return result;
		}
		Enumeration keyEnum = props.keys();
		while(keyEnum.hasMoreElements())
		{
			String key = (String)keyEnum.nextElement();
			String value = props.getProperty(key);
			value = EtcConfig.filter(value);
			result.put(key, value);
		}
		return result;
	}
	
	public static Map<String,String> transformMap(Map<String,String> map)
	{
		Map<String,String> result = new HashMap<String,String>();
		if(map==null || map.isEmpty())
		{
			return result;
		}
		Iterator keyIter = map.keySet().iterator();
		while(keyIter.hasNext())
		{
			String key = (String)keyIter.next();
			String value = map.get(key);
			value = EtcConfig.filter(value);
			result.put(key, value);
		}
		return result;
	}
	
	public static HashMap<String,String> transformMap(HashMap<String,String> map)
	{
		HashMap<String,String> result = new HashMap<String,String>();
		if(map==null || map.isEmpty())
		{
			return result;
		}
		Iterator keyIter = map.keySet().iterator();
		while(keyIter.hasNext())
		{
			String key = (String)keyIter.next();
			String value = map.get(key);
			value = EtcConfig.filter(value);
			result.put(key, value);
		}
		return result;
	}
	
	public static String getPropertyValue(Properties props,String propName,String defaultValue)
	{
		if(props==null || propName==null) return defaultValue;
		String value = props.getProperty(propName);
		if(value!=null)
		{
			value = EtcConfig.filter(value);
			return value;
		}
		else
		{
			if(defaultValue!=null)
				return defaultValue.trim();
			else
				return "";
		}
	}
	
	public static boolean getPropertyBooleanValue(Properties props,String propName,String defaultValue)
	{
		String temp = getPropertyValue(props,propName,defaultValue);
		if(temp.equalsIgnoreCase("true") || temp.equalsIgnoreCase("1"))
		{
			return true;
		}
		return false;
	}
	
	public static int getPropertyIntValue(Properties props,String propName,String defaultValue)
	{
		String temp = getPropertyValue(props,propName,defaultValue);
		return StringUtil.getIntValue(temp);
	}
	
	public static long getPropertyLongValue(Properties props,String propName,String defaultValue)
	{
		String temp = getPropertyValue(props,propName,defaultValue);
		return StringUtil.getLongValue(temp);
	}
	
}
